package com.ryoma.Utils;

import com.ryoma.model.Log;
import com.ryoma.model.elevator.ElevatorRealTimeDatas;
import com.ryoma.model.environment.EnvironmentRealTimeDatas;
import com.ryoma.model.gps.GpsInfos;
import com.ryoma.model.towercrane.TowerRealTimeDatas;
import com.ryoma.service.LogService;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.*;
import java.lang.reflect.Array;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class CommonUtils {

    @Autowired
    private static LogService logService;

    private static Properties properties = null;

    private static ThreadPoolExecutor threadPoolExecutor = null;

    private static final String FILEPATH = "src/development/resources/setting.properties";

    public static Properties getProperties(){
        if(properties==null){
            return loadProperties(FILEPATH);
        }
        return properties;
    }
    /**
     * 提供配置文件路径
     * @param filePath
     * @return
     */
    public static Properties loadProperties(String filePath){
        Properties pro = new Properties();
        FileInputStream in = null;
        try {
            in = new FileInputStream(filePath);
            BufferedReader bf = new BufferedReader(new InputStreamReader(in,"UTF-8"));
            pro.load(bf);
            in.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        properties = pro;
        return pro;
    }

    public static ThreadPoolExecutor getThreadPoolExecutor(){
        if(threadPoolExecutor == null){
            if(properties == null){
                getProperties();
            }
            threadPoolExecutor = new ThreadPoolExecutor(Integer.parseInt(properties.getProperty("corePoolSize").toString()),
                    Integer.parseInt(properties.getProperty("maxmumPoolSize").toString()),Integer.parseInt(properties.getProperty("keepAliveTime").toString()),
                    TimeUnit.SECONDS,new SynchronousQueue<Runnable>());
        }
        return threadPoolExecutor;
    }

    public static String unicodeEncode(String src) throws Exception {
        char c;
        StringBuilder str = new StringBuilder();
        int intAsc;
        String strHex;
        for (int i = 0; i < src.length(); i++) {
            c = src.charAt(i);
            intAsc = (int) c;
            strHex = Integer.toHexString(intAsc);
            if (intAsc > 128)
                str.append("\\u" + strHex);
            else
                str.append("\\u00" + strHex); // 低位在前面补00
        }
        return str.toString();
    }

    /**
     * Unicode解码成字符串
     * @param src
     * @return
     */
    public static String unicodeDecode(String src) {
        int t =  src.length() / 6;
        StringBuilder str = new StringBuilder();
        for (int i = 0; i < t; i++) {
            String s = src.substring(i * 6, (i + 1) * 6); // 每6位描述一个字节
            // 高位需要补上00再转
            String s1 = s.substring(2, 4) + "00";
            // 低位直接转
            String s2 = s.substring(4);
            // 将16进制的string转为int
            int n = Integer.valueOf(s1, 16) + Integer.valueOf(s2, 16);
            // 将int转换为字符
            char[] chars = Character.toChars(n);
            str.append(new String(chars));
        }
        return str.toString();
    }


    public static String string2Sha1(String str){
        if(str==null||str.length()==0){
            return null;
        }
        char hexDigits[] = {'0','1','2','3','4','5','6','7','8','9',
                'a','b','c','d','e','f'};
        try {
            MessageDigest mdTemp = MessageDigest.getInstance("SHA1");
            mdTemp.update(str.getBytes("UTF-8"));

            byte[] md = mdTemp.digest();
            int j = md.length;
            char buf[] = new char[j*2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                buf[k++] = hexDigits[byte0 >>> 4 & 0xf];
                buf[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(buf);
        } catch (Exception e) {
            // TODO: handle exception
            return null;
        }
    }



    public static void bubbleSort(String[] arr) {
        if (arr == null || arr.length == 0)
            return;
        for (int i = 0; i < arr.length - 1; i++) {
            // 循环之后下标i处为数组中最小的元素
            for (int j = arr.length - 1; j > i; j--) {
                if (!compareString(arr[j], arr[j - 1])) {
                    swap(arr, j - 1, j);
                }
            }
        }
    }

    /**
     * 比较两个字符串大小
     *
     * @param param1
     * @param param2
     * @return true:param1 > param2 false:param1 <= param2;
     */
    private static boolean compareString(String param1, String param2) {
        char[] paramC1 = param1.toCharArray();
        char[] paramC2 = param2.toCharArray();
        // 获取最短字符串长度
        int minLength = paramC1.length < paramC2.length ? paramC1.length
                : paramC2.length;
        for (int i = 0; i < minLength; i++) {
            if (paramC1[i] == paramC2[i]) {

            } else if (paramC1[i] > paramC2[i]) {
                return true;
            } else {
                return false;
            }
        }
        return paramC1.length > paramC2.length;

    }
    /**
     * 交换元素
     *
     * @param arr 数组
     * @param i
     *            数组下标
     * @param j
     *            数组下标
     */
    public static void swap(String[] arr, int i, int j) {
        String temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }


    public static void insertIntoLog(Log log){
        logService.insert(log);
    }
}
